package gui.Threading;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingUtilities;

public class Threading extends JFrame {

	
	public Threading(){
		super("Threading");
		 setLocationRelativeTo(null);
		 JPanel pane = new JPanel();
		 add(pane);
		 JButton button = new JButton();
		 pane.add(button);
		 final JProgressBar bar = new JProgressBar();
		 pane.add(bar);
		 setVisible(true);
		 pack();
		 
		 button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				
				new Thread(){
					public int i;
					public void run() {
					for( i=0;i<=100;i++){
						
						System.out.println(i);
						try {
							Thread.sleep((int)(Math.random()*100));
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						try {
							SwingUtilities.invokeAndWait(new Runnable(){								
							public void run(){
								bar.setValue(i);
								
								
							}
							});
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}					}
						
						
					}
					
					
				}.start();
				
				
				
			}
		});
		
	}
	
	public static void main(String[] args) {

		JFrame frame = new Threading();
		
	}

}
